package com.heima.admin.controller.v1;

import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.AdChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/v1/channel")
@Api(value = "ChannelController",tags = "频道管理API")
public class ChannelController {

    @Autowired
    private ChannelService channelService;

    //list接口根据名称查询分页列表
    @PostMapping("/list")
    @ApiOperation("根据频道名称分页查询")
    public ResponseResult querylist(@RequestBody AdChannelDto adChannelDto){
        return channelService.findByNameAndPage(adChannelDto);
    }


    //插入频道
    @PostMapping("/save")
    @ApiOperation("插入频道")
    public ResponseResult save(@RequestBody AdChannel adChannel)
    {
        return channelService.insert(adChannel);
    }


    //修改频道
    @PostMapping("/update")
    @ApiOperation("修改频道")
    public ResponseResult update(@RequestBody AdChannel adChannel){

        if(adChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,
                    "要修改的频道不存在"); }
        channelService.updateById(adChannel);
        return ResponseResult.okResult();

    }



    //删除频道
    @PostMapping("/delete")
    @ApiOperation("删除频道")
    public ResponseResult delete(Integer id) {

        return channelService.delete(id);

    }
}
